<?php

namespace App\Service\Message;

use App\Constants\ErrorCode;
use App\Helpers\ConfigUtil;
use App\Helpers\ImUtil;
use App\Model\GiftLog;
use App\Model\PayOrder;
use App\Model\User;
use App\Model\UserBlacklist;
use App\Model\UserChat;
use App\Model\UserMsgLog;
use App\Model\UserProfile;
use App\Service\BaseService;

/**
 * 聊天框信息
 */
class ChatInfoService extends BaseService
{
    //单向聊天限制
    protected $msgLmt = 3;

    protected function RoVerifyList()
    {
        return [
            'chat_im_account' => 'required|string'
        ];
    }

    public function handle()
    {
        $userId = $this->getUserId();
        $user = $this->getUser();
        $chatImAccount = $this->request->input('chat_im_account', '');
        $userIds = User::getUserIdByIm($chatImAccount, 'id');
        $chatUserId = $userIds[0] ?? 0;
        if (!$chatUserId || $chatUserId == $userId) {
            return $this->setError(ErrorCode::USER_NOT_VALID, '参数传入有误~');
        }
        $userProfile = UserProfile::getProfile($chatUserId);
        //自己是否声优
        $isHost = UserProfile::checkIsHost($userId);

        //聊友用户信息
        $ret['self_user_id'] = $userId;
        $ret['is_host'] = $isHost;
        $ret['chat_user_id'] = $chatUserId;
        $ret['user_info'] = UserProfile::buildProfileDetail($userProfile);
        //当前聊天数
        $userChat = UserChat::check($userId, $chatUserId);
        $ret['msg_num'] = UserChat::pickMsgNum($userChat, $userId);
        $ret['chat_msg_num'] = UserChat::pickMsgNum($userChat, $chatUserId);

        //聊天私信数限制提示
        $ret['msg_limit'] = $ret['pic_limit'] = 0;
        $ret['msg_limit_tip'] = '对方回复你之前，仅可发送3条私信哦！';
        $ret['pic_limit_tip'] = '完成充值，本月才可以发送图片哦！';

        //是否被对方拉黑
        $ret['is_blocked'] = UserBlacklist::check($chatUserId, $userId) ? 1 : 0;
        //是否拉黑对方
        $ret['is_blocking'] = UserBlacklist::check($userId, $chatUserId) ? 1 : 0;
        //是否展示语聊按钮
        $ret['can_call'] = UserProfile::checkCanCall($ret['user_info']['role'], $userProfile->permit);
        //是否展示视频按钮
        $ret['can_video'] = UserProfile::checkCanVideo($ret['user_info']['role'], $userProfile->video_permit);

        //声优无限制
        if ($isHost) {
            //礼物过期提示
            $ret['exp_num'] = GiftLog::expireRecords($chatUserId, $userId);
            if ($ret['exp_num'] > 0) {
                ImUtil::sendInfoTipMsg($user->im_account, '您没有及时回复消息，打招呼礼物已被退回～', $chatImAccount);
            }
            log_user_result($userId, $ret);
            return $ret;
        }

        //当天充值的无限制
        $checkRecharge = PayOrder::checkSuccess($userId, date('Y-m-d'));
        if ($checkRecharge) {
            $ret['pay_success'] = $checkRecharge->order_id;
            log_user_result($userId, $ret);
            return $ret;
        }

        //未回复：限制3条
        if (
            $ret['chat_msg_num'] == 0 && $ret['msg_num'] >= ConfigUtil::getChat('msg_limit_noback')
        ) {
            $ret['msg_limit'] = $ret['pic_limit'] = 1;
            $ret['msg_limit_tip'] = $ret['pic_limit_tip'] = '对方回复你之前，仅可发送3条私信哦！';
            log_user_result($userId, $ret);
            return $ret;
        }

        //每天最多50条
        $msgUserNum = UserMsgLog::getSendMsgNum($userId, 0, date('Y-m-d'));
        $msgMaxLmt = ConfigUtil::getChat('msg_limit');
        if ($msgUserNum >= $msgMaxLmt) {
            $ret['msg_limit'] = $ret['pic_limit'] = 1;
            $ret['tip_code'] = ErrorCode::CHAT_DAILY_LIMIT;
            $ret['msg_limit_tip'] = $ret['pic_limit_tip'] = '免费私聊次数已用完，今天完成任意金额充值即可无限发送私信哦～';
        }

        log_user_result($userId, $ret);
        return $ret;
    }
}
